package com.hakim.wfb.admin.service.impl;

import com.hakim.wfb.admin.enums.ErrorCodeEnum;
import com.hakim.wfb.admin.params.admin.GetAdminMenuParam;
import com.hakim.wfb.admin.params.admin.LoginParam;
import com.hakim.wfb.admin.params.admin.LogoutParam;
import com.hakim.wfb.admin.result.admin.AdminInfoView;
import com.hakim.wfb.admin.result.admin.AdminMenuView;
import com.hakim.wfb.admin.service.api.SystemUserActionService;
import com.hakim.wfb.bean.Menu;
import com.hakim.wfb.dao.AdminDao;
import com.hakim.wfb.dao.AdminRoleDao;
import com.hkfs.fundamental.api.data.ListResultEx;
import com.hkfs.fundamental.api.data.ObjectResultEx;
import com.hkfs.fundamental.api.data.ResultEx;
import com.hkfs.fundamental.api.params.SessionIdentity;
import com.hkfs.fundamental.common.utils.StrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 */
@Service("systemUserActionService")
public class SystemUserActionServiceImpl implements SystemUserActionService {

    private final Logger logger = LoggerFactory.getLogger(SystemUserActionServiceImpl.class);

    @Autowired
    private AdminDao adminDao;

    @Autowired
    private AdminRoleDao adminRoleDao;

    /**
     * 用户登录
     *
     * @param param
     * @return
     */
    public ObjectResultEx<SessionIdentity> login(LoginParam param) {
        ObjectResultEx<SessionIdentity> result = new ObjectResultEx<SessionIdentity>();
        List<AdminInfoView> list = adminDao.getUserByUsername(param.getUserName());
        if (list.size() == 0) {
            return result.makeResult(ErrorCodeEnum.LOGIN_USER_NOT_EXISTS.getCode(), ErrorCodeEnum.LOGIN_USER_NOT_EXISTS.getMessage());
        }
        if (list.size() > 1) {
            return result.makeResult(ErrorCodeEnum.LOGIN_DUPLICATE_USERNAME.getCode(), ErrorCodeEnum.LOGIN_DUPLICATE_USERNAME.getMessage());
        }
        AdminInfoView userInfo = list.get(0);
        if (userInfo.getOptlock() != null && userInfo.getOptlock() > 0) {
            return result.makeResult(ErrorCodeEnum.LOGIN_USER_LOCKED.getCode(), ErrorCodeEnum.LOGIN_USER_LOCKED.getMessage());
        }
        String md5pwd = StrUtils.getMd5(param.getPassword());
        if (!md5pwd.equals(userInfo.getPassword())) {
            return result.makeResult(ErrorCodeEnum.LOGIN_PASSWORD_NOT_MATCH.getCode(), ErrorCodeEnum.LOGIN_PASSWORD_NOT_MATCH.getMessage());
        }
        //获取用户角色列表
        List<String> roles = adminRoleDao.getRoleIdsByUserId(userInfo.getId());

        SessionIdentity sessionIdentity = new SessionIdentity();
//        sessionIdentity.setRoles(new String[]{userInfo.getRoleName()});
        sessionIdentity.setRoles(roles.toArray(new String[roles.size()]));
        sessionIdentity.setName(userInfo.getUsername());
        sessionIdentity.setUserId(userInfo.getId());
        result.setDataObject(sessionIdentity);
        return result.makeSuccessResult();
    }

    /**
     * 用户登出
     *
     * @param param
     * @return
     */
    public ResultEx logout(LogoutParam param) {
        return null;
    }

    /**
     * 获取用户菜单
     *
     * @param param
     * @return
     */
    public ListResultEx<AdminMenuView> getUserMenu(GetAdminMenuParam param) {
        ListResultEx<AdminMenuView> result = new ListResultEx<AdminMenuView>();
//        List<Menu> menuList1 = adminDao.getMenuByUserId(param.getSessionIdentity().getUserId());
        List<Menu> menuList = adminDao.getMenuByUserId2(param.getSessionIdentity().getUserId());

        if (menuList.size() == 0) {
            result.makeFailedResult("没有菜单记录");
        }
        List<AdminMenuView> adminMenuList = new ArrayList<AdminMenuView>();
        for (int i = 0; i < menuList.size(); i++) {
            Menu menu = menuList.get(i);
            //顶级菜单
            if (menu.getParentId() == null || menu.getParentId() == 0) {
                AdminMenuView adminMenuView = new AdminMenuView();
                adminMenuView.setId(menu.getId());
                adminMenuView.setMenuName(menu.getMenuName());
                adminMenuView.setMenuPath(menu.getMenuPath());
                adminMenuView.setOrderNum(menu.getOrderNum());
                List<AdminMenuView> childMenuList = new ArrayList<>();
                adminMenuView.setChildMenus(childMenuList);
                adminMenuList.add(adminMenuView);

            } else {
                AdminMenuView adminMenuView = null;
                List<AdminMenuView> childMenuList = null;
                for (int j = 0; j < adminMenuList.size(); j++) {
                    if (menu.getParentId() == adminMenuList.get(j).getId()) {
                        adminMenuView = adminMenuList.get(j);
                        break;
                    }
                }
                if (adminMenuView != null) {
                    childMenuList = adminMenuView.getChildMenus();
                    AdminMenuView child = new AdminMenuView();
                    child.setId(menu.getId());
                    child.setMenuName(menu.getMenuName());
                    child.setMenuPath(menu.getMenuPath());
                    child.setOrderNum(menu.getOrderNum());
                    childMenuList.add(child);
                }
            }
        }
        result.setDataList(adminMenuList);
        return result.makeSuccessResult();
    }
}
